package com.java.mail;

import java.io.FileInputStream;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JavaMailTest2 {
	public static void main(String[] args) throws Exception {
		sendMail();
		//System.exit(0);
	}
	public static void sendMail() throws Exception{
		Properties props = new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", "smtp.163.com");
		Session session = Session.getInstance(props,new Authenticator(){
			protected PasswordAuthentication getPasswordAuthentication(){
				return new PasswordAuthentication("samsara0650","Adgsqwj0650");
			}
		});
		session.setDebug(true);
		
/*		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress("samsara0650@163.com"));
		//msg.setFrom();
		msg.setSubject("send mail!!!");
		msg.setContent("<span style='color:red;'>fuck you</span>", "text/html;charset=gbk");
		//发送 多收件人
		//msg.setRecipient(RecipientType.TO, new InternetAddress("samsara0650@163.com"));
		msg.setRecipients(RecipientType.TO, InternetAddress.parse("samsara0650@163.com,935256169@qq.com"));
		//抄送
		//msg.setRecipient(RecipientType.CC, new InternetAddress("9352561693@qq.com"));
		//密送
		//msg.setRecipient(RecipientType.BCC, new InternetAddress("9352561693@qq.com"));
		Transport.send(msg);*/
		
		Message msg = new MimeMessage(session,new FileInputStream("D:\\temp\\demo3.eml"));
		//要重新设置收件人地址 只发送到这个地址
		Transport.send(msg,InternetAddress.parse("Minimalist.life1989@gmail.com"));
		
		
		
	}
	//发送正确
/*	DEBUG: setDebug: JavaMail version 1.4.7
	DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
	DEBUG SMTP: useEhlo true, useAuth true
	DEBUG SMTP: useEhlo true, useAuth true
	DEBUG SMTP: trying to connect to host "smtp.163.com", port 25, isSSL false
	220 hz-b-163smtp1.163.com SMTP Server for Netease [466] 3a3948cc-b9c4-4dad-89b6-bd67e7310823
	DEBUG SMTP: connected to host "smtp.163.com", port: 25

	EHLO Minimalist-PC
	250-hz-b-163smtp1.163.com
	250-mail
	250-PIPELINING
	250-8BITMIME
	250-AUTH LOGIN PLAIN
	250-AUTH=LOGIN PLAIN
	250 STARTTLS 3a3948cc-b9c4-4dad-89b6-bd67e7310823
	DEBUG SMTP: Found extension "mail", arg ""
	DEBUG SMTP: Found extension "PIPELINING", arg ""
	DEBUG SMTP: Found extension "8BITMIME", arg ""
	DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
	DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
	DEBUG SMTP: Found extension "STARTTLS", arg "3a3948cc-b9c4-4dad-89b6-bd67e7310823"
	DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
	DEBUG SMTP: AUTH LOGIN command trace suppressed
	DEBUG SMTP: AUTH LOGIN succeeded
	DEBUG SMTP: use8bit false
	MAIL FROM:<samsara0650@163.com>
	250 2.0.0 OK 3a3948cc-b9c4-4dad-89b6-bd67e7310823
	RCPT TO:<samsara0650@163.com>
	250 2.0.0 OK 3a3948cc-b9c4-4dad-89b6-bd67e7310823
	RCPT TO:<935256169@qq.com>
	250 2.0.0 OK 3a3948cc-b9c4-4dad-89b6-bd67e7310823
	DEBUG SMTP: Verified Addresses
	DEBUG SMTP:   samsara0650@163.com
	DEBUG SMTP:   935256169@qq.com
	DATA
	354 Start mail input; end with <CRLF>.<CRLF> 3a3948cc-b9c4-4dad-89b6-bd67e7310823
	From: samsara0650@163.com
	To: samsara0650@163.com, 935256169@qq.com
	Message-ID: <25292190.0.1368238029018.JavaMail.BlueKey@smtp.163.com>
	Subject: send mail!!!
	MIME-Version: 1.0
	Content-Type: text/html;charset=gbk
	Content-Transfer-Encoding: 7bit

	<span style='color:red;'>fuck you</span>
	.
	250 2.0.0 OK 3a3948cc-b9c4-4dad-89b6-bd67e7310823
	QUIT
	221 2.0.0 Bye 3a3948cc-b9c4-4dad-89b6-bd67e7310823*/

}
